package jlib.component.net
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;

	public class MultiLoader extends EventDispatcher
	{
		private var loadModeStrategy:ILoadModeStrategy;
		private var m_mode:String;
		public function MultiLoader(): void
		{
			setLoadMode(MultiLoadMode.SAMETIME);
		}
		
		public function setLoadMode($mode:String=MultiLoadMode.SAMETIME): void
		{
			m_mode = $mode;
			reset();
		}
		
		public function setDelay($time:Number): void
		{
			loadModeStrategy.setDelay($time);
		}
		
		public function getLoaderAt($index:int): SingleLoader
		{
			return loadModeStrategy.getLoaderList()[$index];
		}
		
		public function getLoaderLength(): int
		{
			return loadModeStrategy.getLoaderList().length;
		}
		
		public function getLoadedDataAt($index:int): *
		{
			return loadModeStrategy.getLoadedDataAt($index);
		}
		
		public function getLoadedDataLength(): int 
		{
			return loadModeStrategy.getLoadedDataLength();
		}
		
		public function getLoadedIndex($data:*): void
		{
			loadModeStrategy.getLoadedIndex($data);	
		}
		
		public function removeLoadedDataAt($index:int): void
		{
			loadModeStrategy.removeLoadedDataAt($index);
		}
		
		public function load($requests:Array, $params:Array=null): void 
		{
			loadModeStrategy.load($requests, $params);
		}
		
		public function reset(): void
		{
			if(m_mode == MultiLoadMode.SAMETIME) loadModeStrategy = new SameTimeStrategy();
			else loadModeStrategy = new StepByStepStrategy();
			
			loadModeStrategy.addEventListener(MultiLoadEvent.ALL_COMPLETE, dispatchEvent);
			loadModeStrategy.addEventListener(Event.COMPLETE , dispatchEvent );
			loadModeStrategy.addEventListener(ProgressEvent.PROGRESS, dispatchEvent );
			loadModeStrategy.addEventListener(IOErrorEvent.IO_ERROR , dispatchEvent );
		}
		
	}//c
}